﻿#pragma once
#include "GLLLvlObj.h"


namespace GLL
{
	class Level;
	class Signal;

	enum LevelThreadType
	{
		TT_PRE,
		TT_LOAD,
		TT_LOADED,
		TT_UNLOAD,
		TT_POST,
	};

	//多线程读取用属性：
	class LevelThread
	{
	public:
		LevelThread(Level* p){
			_Level = p;				
			_threadFinished = false;
			_threadInstance = nullptr;
			_1Thread = false;
			_Stop = false; 
		}

		void Start();
		void TheThread( Signal& );
		bool PutToQueue(LvlObjSrdPtr);
		virtual bool DoOnce() = 0;

	public:
		bool _Stop;

		bool _threadFinished;

		/**等待此线程结束.*/
		void WaitingFinished();

		/**是否使用多线程删除.*/
		bool _1Thread;

		/**让这个线程暂时关闭.*/
		void ThreadPause(){_Stop = true;}

		/**恢复.*/
		void ThreadResume(){_Stop = false;}

		void ClearList(){_objQueue.clear();}
	protected:
		virtual LevelThreadType GetThreadType() = 0;
		virtual const char* GetName(){return "LevelThread_Unname";}
		virtual bool PrePut(LvlObjSrdPtr){ return true; }

	protected:
		Level* _Level;
		boost::thread* _threadInstance;
		GLL_Mutex _listMutex;
		typedef std::list<LvlObjSrdPtr> ObjSrdPtrList;
		ObjSrdPtrList _objQueue;
		int QueueSize();
		LvlObjSrdPtr PopObj();
		void SetName();
		void Do();
		void Clear();
		void Continue();
	};

	class LevelThreadPreLoad : public LevelThread
	{
		virtual LevelThreadType GetThreadType(){return TT_PRE;}
		virtual const char* GetName(){return "Pre - Load";}
		virtual bool PrePut(LvlObjSrdPtr){return !_threadFinished;	}
	public:
		virtual bool DoOnce();
		LevelThreadPreLoad(Level* p) : LevelThread(p){_1Thread = true;}		
	};

	class LevelThreadLoad : public LevelThread
	{
		virtual LevelThreadType GetThreadType(){return TT_LOAD;}
		virtual const char* GetName(){return "Level Thread Load";}
		virtual bool PrePut(LvlObjSrdPtr){return !_threadFinished;	}

	public:
		virtual bool DoOnce();
		LevelThreadLoad(Level* p):LevelThread(p){_1Thread = false;}
	};

	class LevelThreadPostLoad : public LevelThread
	{
		virtual LevelThreadType GetThreadType(){return TT_LOADED;}
		virtual const char* GetName(){return "Post - Load";}
		virtual bool PrePut(LvlObjSrdPtr){return !_threadFinished;	}
	public:
		virtual bool DoOnce();
		LevelThreadPostLoad(Level* p) : LevelThread(p){_1Thread = true;}		
	};

	class LevelThreadUnload : public LevelThread
	{
		virtual LevelThreadType GetThreadType(){return TT_UNLOAD;}
		virtual const char* GetName(){return "Level Thread Unload";}
	public:
		virtual bool DoOnce();
		LevelThreadUnload(Level* p):LevelThread(p){_1Thread = true;}
	};

	class LevelThreadPost : public LevelThread
	{
		virtual LevelThreadType GetThreadType(){return TT_POST;}
		virtual const char* GetName(){return "post - unload";}
	public:
		virtual bool DoOnce();
		LevelThreadPost(Level* p) : LevelThread(p){_1Thread = true;}		
	};

};//GLL
